<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bussiness:company:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bussiness:company:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bussiness:company:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bussiness:company:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="companyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="logo" align="center" prop="logo">
        <template slot-scope="scope">
          　　　　<img
            :src="scope.row.logo"
            width="40"
            height="40"
            class="column-pic"
          />
          　　</template
        >
      </el-table-column>
      <el-table-column label="企业名称" align="center" prop="companyName" />
      <el-table-column label="联系人" align="center" prop="linkMain" />
      <el-table-column label="企业地址" align="center" prop="address" />
      <el-table-column label="联系电话" align="center" prop="linkTel" />
      <el-table-column
        label="自定义平台名称"
        align="center"
        prop="platformName"
      />
      <el-table-column label="二级域名" align="center" prop="domain" />
      <el-table-column label="绑定账号" align="center" prop="userName" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bussiness:company:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bussiness:company:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="企业名称" prop="companyName">
              <el-input
                v-model="form.companyName"
                placeholder="请输入企业名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平台名称" prop="platformName">
              <el-input
                v-model="form.platformName"
                placeholder="请输入自定义平台名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二级域名" prop="domain">
              <el-input v-model="form.domain" placeholder="请输入二级域名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入企业地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人" prop="linkMain">
              <el-input v-model="form.linkMain" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="linkTel">
              <el-input v-model="form.linkTel" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业logo" prop="logo">
              <el-upload
                ref="upload"
                :limit="1"
                accept=".jpg, .png"
                :action="upload.url"
                :headers="upload.headers"
                :file-list="upload.fileList"
                :on-progress="handleFileUploadProgress"
                :on-success="handleFileSuccess"
                :on-remove="handlerFileRemove"
                :auto-upload="true"
                list-type="picture"
              >
                <el-button slot="trigger" size="small" type="primary"
                  >选取图片</el-button
                >
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登录背景" prop="loginBgimg">
              <el-upload
                ref="bgImgUpload"
                :limit="1"
                accept=".jpg, .png"
                :action="upload.url"
                :headers="upload.headers"
                :file-list="upload.bgImgFileList"
                :on-progress="handleBgImgFileUploadProgress"
                :on-success="handleBgImgFileSuccess"
                :on-remove="handlerBgImgFileRemove"
                :auto-upload="true"
                list-type="picture"
              >
                <el-button slot="trigger" size="small" type="primary"
                  >选取图片</el-button
                >
              </el-upload>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="管理账号" prop="userId">
              <el-select
                v-model="form.userName"
                filterable
                clearable
                placeholder="请选择管理账号"
                @change="userChange"
                style="width: 100%"
              >
                <el-option
                  v-for="item in userList"
                  :key="item.userId"
                  :label="item.userName"
                  :value="item.userId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="24"
            ><el-divider content-position="left"
              >沭聚物联感知中台配置</el-divider
            ></el-col
          >
          <el-col :span="12">
            <el-form-item label="userId" prop="iotUserId">
              <el-input v-model="form.iotUserId" placeholder="userId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="accessKey" prop="iotAccessKey">
              <el-input v-model="form.iotAccessKey" placeholder="accessKey" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompany,
  getCompany,
  delCompany,
  addCompany,
  updateCompany,
  listUser,
} from "@/api/bussiness/company";
import { getToken } from "@/utils/auth";

import _ from "lodash";

export default {
  name: "Company",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业管理表格数据
      companyList: [],
      //用户列表数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上传参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/file/upload",
        // 上传的文件列表
        fileList: [],
        bgImgFileList: [],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          {
            required: true,
            message: "请输入企业名称",
          },
        ],
        domain: [
          {
            required: true,
            message: "请输入二级域名",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    userChange(userId) {
      this.form.userId = userId;
      let user = _.find(this.userList, (o) => {
        return o.userId == userId;
      });
      this.form.userName = user.userName;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.logo = response.data.url;
      this.$modal.msgSuccess("上传成功");
    },
    //文件移除
    handlerFileRemove() {
      this.form.logo = "";
    },
    // 文件上传中处理
    handleBgImgFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleBgImgFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.form.loginBgImg = response.data.url;
      this.$modal.msgSuccess("上传成功");
    },
    //文件移除
    handlerBgImgFileRemove() {
      this.form.loginBgImg = "";
    },
    /** 查询企业管理列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then((response) => {
        this.companyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        companyName: null,
        address: null,
        linkMain: null,
        linkTel: null,
        logo: null,
        remark: null,
        loginBgImg: null,
        platformName: null,
        domain: null,
        userId: null,
        userName: null,
        iotUserId: null,
        iotAccessKey: null,
      };
      this.upload.fileList = [];
      this.upload.bgImgFileList = [];
      this.resetForm("form");
      this.userList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业";
      this.getUserList();
    },
    //获取用户列表
    getUserList() {
      listUser().then((response) => {
        this.userList = response.data;
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCompany(id).then((response) => {
        this.form = response.data;
        if (this.form.logo) {
          this.upload.fileList = [{ url: this.form.logo }];
        }
        if (this.form.loginBgImg) {
          this.upload.bgImgFileList = [{ url: this.form.loginBgImg }];
        }
        this.open = true;
        this.title = "修改企业";
      });
      this.getUserList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCompany(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除企业管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCompany(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "bussiness/company/export",
        {
          ...this.queryParams,
        },
        `company_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
